$(function(){
    initSubmitEvent();
});
function initSubmitEvent(){
    $('#submitForm').validate({
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function(form){
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
                jQuery.data(this, "disabledOnSubmit", {submited : true});
                //异步提交
                var requestJson = $(form).serializeJSON();
                var that = this;
                if(confirm("确认修改商品嘛？")) {
                    $.ajax({
                        url: form.action,
                        type: "POST",
                        contentType: 'application/json;charset=utf-8', //设置请求头信息
                        dataType: "json",
                        data: JSON.stringify(requestJson),
                        success: function (data) {
                            if (data.code == 0) {
                                alert(data.msg);
                                window.location.href = appServer + "/goods/list.htm";
                            } else {
                                alert(data.msg);
                                jQuery.removeData(that, "disabledOnSubmit");
                            }
                        },
                        error: function (res) {
                            alert(res.responseText);
                            jQuery.removeData(that, "disabledOnSubmit");
                        }
                    });
                }else{
                    jQuery.removeData(that, "disabledOnSubmit");
                }
            } else {
                // repeat submit ignore;
            }
        },
        success:function(element){
        },
        errorPlacement : function(error, element) {
            if(error.text()){
                if(element.next("span[class='red']").size() == 0){
                    element.after("<span class='red'>" + error.text() +"</span>");
                }else{
                    element.next("span[class='red']").text(error.text());
                }
            }else{
                element.next("span[class='red']").remove();
            }
        },
        rules:{
            "goodsName":{required:true},
            "classifyId":{required:true},
            "costPrice":{required:true},
            "organizationId":{required:true},
            "images-flag":{required:true,min:1},
            "tradeUnit":{isInteger:true}
        },
        messages:{
            "images-flag":{required:"请上传商品轮播图片,至少一张",min:"请上传商品轮播图片,至少一张"},
            "goodsName":{required:"商品名称必填"},
            "classifyId":{required:"二级分类必填"},
            "costPrice":{required:"折扣价必填"},
            "organizationId":{required:"供应商必填"},
            "tradeUnit":{isInteger:"只能输入正整数"}

        }
    });
}